package edu.ccut.saturn.studio.wfg.dialog.formdatadialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.wfg.wfgmodel.ExtendAttributeForFormData;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnAttribute;

public class SaturnFormDataParameterAddDialog extends TitleAreaDialog {
	
	private Text optionText, valueText;
	
	private Label optionLabel, valueLabel;
	
	private Button appointValueCheckBox;
	
	private List<SaturnAttribute> attributes;
	
	private ExtendAttributeForFormData extendAttribute;

	public SaturnFormDataParameterAddDialog(Shell parent, List<SaturnAttribute> attributes) {
		super(parent);
		this.attributes = attributes;
		this.extendAttribute = new ExtendAttributeForFormData();
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("SetFromParameter"));   //$NON-NLS-1$
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		final Composite composite = new Composite(area, SWT.NONE);
		
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true);
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);

		optionLabel = new Label(composite, SWT.NONE);
		final GridData gd_optionLabel = new GridData();
		gd_optionLabel.verticalIndent = 5;
		gd_optionLabel.horizontalIndent = 8;
		optionLabel.setLayoutData(gd_optionLabel);
		optionLabel.setText(Messages.getString("Oprion"));   //$NON-NLS-1$

		optionText = new Text(composite, SWT.BORDER);
		optionText.addModifyListener(new ModifyListener() {
			@SuppressWarnings("deprecation") //$NON-NLS-1$
			public void modifyText(final ModifyEvent e) {
				if(appointValueCheckBox.getSelection()==false){
					valueText.setText(optionText.getText());
				}
				if(!("".equals(optionText.getText()))){ //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				} else if("".equals(optionText.getText())){ //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
			}
		});
		final GridData gd_optionText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_optionText.verticalIndent = 5;
		gd_optionText.horizontalIndent = 12;
		optionText.setLayoutData(gd_optionText);

		appointValueCheckBox = new Button(composite, SWT.CHECK);
		appointValueCheckBox.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(appointValueCheckBox.getSelection()==false){
					valueText.setEnabled(false);
				}if(appointValueCheckBox.getSelection()==true){
					valueText.setEnabled(true);
				}
			}
		});
		final GridData gd_appointValueCheckBox = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
		gd_appointValueCheckBox.verticalIndent = 5;
		gd_appointValueCheckBox.horizontalIndent = 8;
		appointValueCheckBox.setLayoutData(gd_appointValueCheckBox);
		appointValueCheckBox.setText(Messages.getString("selectedValue"));   //$NON-NLS-1$
		appointValueCheckBox.setSelection(false);

		valueLabel = new Label(composite, SWT.NONE);
		final GridData gd_valueLabel = new GridData();
		gd_valueLabel.verticalIndent = 5;
		gd_valueLabel.horizontalIndent = 8;
		valueLabel.setLayoutData(gd_valueLabel);
		valueLabel.setText(Messages.getString("Value"));   //$NON-NLS-1$

		valueText = new Text(composite, SWT.BORDER);
		final GridData gd_valueText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_valueText.verticalIndent = 5;
		gd_valueText.horizontalIndent = 12;
		valueText.setLayoutData(gd_valueText);
		valueText.setEnabled(false);
		
		return area;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(360, 240);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if(buttonId == IDialogConstants.OK_ID){
			extendAttribute.setOptionItem(optionText.getText().trim());
			extendAttribute.setOptionValue(valueText.getText().trim());
			attributes.add(extendAttribute);
		}
		super.buttonPressed(buttonId);
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
}
